package com.sheepone.distributor.config;


import com.sheepone.distributor.util.NameGenerator;
import com.sheepone.util.algorithm.analysis.IntegerAnalysisUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Miss.杨
 * @description
 * @since 2023-12-07 22:04
 */
public class DistributorLineConfig {
    protected String name = "Distributor";

    protected int size = 1024;

    // 只做转发 没必要另外分配线程池 所以没必要加上对于线程池的配置
    protected int handlerNum = 4;
    protected int intervalTime = DISABLE_DATA_MONITOR;

    protected String[] listeners;

    protected boolean enableDelay = false;

    protected static int DISABLE_DATA_MONITOR = 0;

    public static boolean enableDataMonitor(int intervalTime) {
        return intervalTime > DISABLE_DATA_MONITOR;
    }

    public String[] getListeners() {
        return listeners;
    }

    public void setListeners(String[] listeners) {
        this.listeners = listeners;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getHandlerNum() {
        return handlerNum;
    }

    public void setHandlerNum(int handlerNum) {
        this.handlerNum = handlerNum;
    }

    public int getIntervalTime() {
        return intervalTime;
    }

    public void setIntervalTime(int intervalTime) {
        this.intervalTime = intervalTime;
    }

    public boolean isEnableDelay() {
        return enableDelay;
    }

    public void setEnableDelay(boolean enableDelay) {
        this.enableDelay = enableDelay;
    }


    public static DistributorLineConfigXmlBuilder builder() {
        return new DistributorLineConfigXmlBuilder();
    }


    public static class DistributorLineConfigXmlBuilder {
        private final DistributorLineConfig config = new DistributorLineConfig();
        private static final Logger logger = LoggerFactory.getLogger(new Object() {
        }.getClass().getEnclosingClass());

        public DistributorLineConfigXmlBuilder name(String name) {
            config.setName(StringUtils.isBlank(name) ? NameGenerator.generateDistributorName() : name);
            return this;
        }

        public DistributorLineConfigXmlBuilder size(String size) {
            if (!StringUtils.isBlank(size)) {
                if (IntegerAnalysisUtil.isPositiveInteger((size))) {
                    config.setSize(Integer.parseInt(size));
                } else {
                    throw new IllegalArgumentException("size must be a positive integer");
                }
            }
            return this;
        }

        public DistributorLineConfigXmlBuilder handlerNum(String handlerNum) {
            if (!StringUtils.isBlank(handlerNum)) {
                if (IntegerAnalysisUtil.isPositiveInteger((handlerNum))) {
                    int time = Integer.parseInt(handlerNum);
                    config.setHandlerNum(time);
                } else {
                    throw new IllegalArgumentException("handlerNum must be a positive integer");
                }
            }

            return this;
        }

        public DistributorLineConfigXmlBuilder intervalTime(String intervalTime) {
            if (!StringUtils.isBlank(intervalTime)) {
                if (IntegerAnalysisUtil.isInteger(intervalTime)) {
                    int time = Integer.parseInt(intervalTime);
                    if (enableDataMonitor(time)) {
                        config.setIntervalTime(time);
                    } else if (logger.isWarnEnabled()) {
                        logger.warn("If you want to enable the timed monitoring function, the value of intervalTime must be a positive integer");
                    }
                } else {
                    throw new IllegalArgumentException("intervalTime must be a integer");
                }
            }
            return this;
        }

        public DistributorLineConfigXmlBuilder listeners(String[] listeners) {
            if (listeners != null && listeners.length > 0) {
                config.setListeners(listeners);
            }
            return this;
        }

        public DistributorLineConfigXmlBuilder enableDelay(boolean enableDelay) {
            config.setEnableDelay(enableDelay);
            return this;
        }

        public DistributorLineConfig build() {
            return config;
        }
    }
}


